package www.wen.com.common.lib.util;

import android.os.Parcelable
import com.tencent.mmkv.MMKV
import www.wen.com.common.lib.helper.loading.KeyHelper.MMKV_IS_FIRST_SHOW_AGREEMENT_POPU_VIEW
import java.util.*

/**
 * @author WEN
 * @Description:
 * @date 2021/1/28 21:32
 */
object SpUtil {

    var mmkv: MMKV? = null

    init {
        mmkv = MMKV.defaultMMKV()
    }

    fun encode(key: String, value: Any?) {
        when (value) {
            is String -> mmkv?.encode(key, value)
            is Float -> mmkv?.encode(key, value)
            is Boolean -> mmkv?.encode(key, value)
            is Int -> mmkv?.encode(key, value)
            is Long -> mmkv?.encode(key, value)
            is Double -> mmkv?.encode(key, value)
            is ByteArray -> mmkv?.encode(key, value)
            is Nothing -> return
        }
    }

    fun <T : Parcelable> encode(key: String, t: T?) {
        if(t ==null){
            return
        }
        mmkv?.encode(key, t)
    }

    fun encode(key: String, sets: Set<String>?) {
        if(sets ==null){
            return
        }
        mmkv?.encode(key, sets)
    }

    fun decodeInt(key: String,defaultValue:Int = -1): Int? {
        return mmkv?.decodeInt(key, defaultValue)
    }

    fun decodeDouble(key: String): Double? {
        return mmkv?.decodeDouble(key, 0.00)
    }

    fun decodeLong(key: String,value: Long = -1L): Long? {
        return mmkv?.decodeLong(key, value)
    }

    fun decodeBoolean(key: String,defaultValue: Boolean = false): Boolean {
        return mmkv?.decodeBool(key, defaultValue)!!
    }

    fun decodeFloat(key: String): Float? {
        return mmkv?.decodeFloat(key, 0F)
    }

    fun decodeByteArray(key: String): ByteArray? {
        return mmkv?.decodeBytes(key)
    }

    fun decodeString(key: String,defaultValue:String = ""): String? {
        return mmkv?.decodeString(key, defaultValue)
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return mmkv?.decodeParcelable(key, tClass)
    }

    fun decodeStringSet(key: String): Set<String>? {
        return mmkv?.decodeStringSet(key, Collections.emptySet())
    }

    fun removeKey(key: String) {
        mmkv?.removeValueForKey(key)
    }

    fun clearAll() {
//        mmkv?.clearAll()
        //这里写无需清除的key
        val args = arrayOf(MMKV_IS_FIRST_SHOW_AGREEMENT_POPU_VIEW)
        mmkv?.allKeys()?.filter { key1->
            var isIn = true
            args.forEach { key2->
                if(key1 == key2){
                    isIn = false
                    return@forEach
                }
            }
            isIn
        }.run {
            this?.forEach {
                removeKey(it)
            }
        }
    }

    fun clearAllExcludeKey(vararg args:String) {
        mmkv?.allKeys()?.filter { key1->
            var isIn = false
            args.forEach { key2->
                if(key1 == key2){
                    isIn = true
                    return@forEach
                }
            }
            isIn
        }.run {
            this?.forEach {
                removeKey(it)
            }
        }

    }

}